//
//  RETProductSkuCell.m
//  Retail
//
//  Created by ChenJie on 4/1/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "RETProductSkuCell.h"
#import "OABase.h"
#import "RETSkuItemView.h"

typedef NS_ENUM(NSUInteger, ComponentId) {
    TitleLabel,
    ItemContainer,
    ItemFrames,
};

@interface RETProductSkuCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *itemContainer;
@property (nonatomic, strong) NSMutableArray<RETSkuItemView *> *itemViews;

@end

@implementation RETProductSkuCell

#pragma mark - Lifecycle methods

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = OAColor(ffffff);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.itemContainer];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //
    self.titleLabel.frame = [self.viewModel.oa_layouts[@(TitleLabel)] CGRectValue];
    self.itemContainer.frame = [self.viewModel.oa_layouts[@(ItemContainer)] CGRectValue];
    
    NSMutableArray *skuItemFrames = self.viewModel.oa_layouts[@(ItemFrames)];
    for (NSInteger i = 0; i < self.itemContainer.subviews.count; i++) {
        RETSkuItemView *skuItemView = self.itemContainer.subviews[i];
        CGRect skuItemFrame = [skuItemFrames[i] CGRectValue];
        skuItemView.frame = skuItemFrame;
    }
    
//    [self oa_showBounds];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    //
}

#pragma mark - Public methods

#define kTitleMarginTop 40
#define kContainerMarginTop 20
#define kTitleFont [UIFont systemFontOfSize:18]
#define kSpaceBetweenSkuItemH 20
#define kSpaceBetweenSkuItemV 16
#define kSkuItemCountPerRow 4
#define kSkuItemHeight 40

+ (CGFloat)oa_heightWithWidth:(CGFloat)cellWidth viewModel:(RETProductSkuCellViewModel *)viewModel {
    NSMutableDictionary *layouts = [NSMutableDictionary dictionary];
    CGFloat topMark = 0;
    
    topMark += kTitleMarginTop;
    CGSize titleSize = [viewModel.title oa_sizeWithWidth:cellWidth font:kTitleFont];
    CGRect titleFrame = CGRectMake(0, topMark, titleSize.width, titleSize.height);
    layouts[@(TitleLabel)] = OAValueWithRect(titleFrame);
    topMark += titleSize.height;
    
    topMark += kContainerMarginTop;
    NSInteger rowCount = 0;
    if (viewModel.items.count > 0) {
        rowCount = ((viewModel.items.count - 1) / kSkuItemCountPerRow) + 1;
    }
    CGFloat containerHeight = kSkuItemHeight * rowCount + kSpaceBetweenSkuItemV * (rowCount - 1);
    CGRect containerFrame = CGRectMake(0, topMark, cellWidth, containerHeight);
    layouts[@(ItemContainer)] = OAValueWithRect(containerFrame);
    
    CGFloat skuItemWidth = (cellWidth - kSpaceBetweenSkuItemH * (kSkuItemCountPerRow - 1)) / kSkuItemCountPerRow;
    NSMutableArray *skuItemFrames = [NSMutableArray array];
    for (NSInteger i = 0; i < viewModel.items.count; i++) {
        NSInteger rowIndex = i / kSkuItemCountPerRow;
        NSInteger colIndex = i % kSkuItemCountPerRow;
        CGFloat itemLeft = colIndex * (skuItemWidth + kSpaceBetweenSkuItemH);
        CGFloat itemTop = rowIndex * (kSkuItemHeight + kSpaceBetweenSkuItemV);
        CGRect itemFrame = CGRectMake(itemLeft, itemTop, skuItemWidth, kSkuItemHeight);
        [skuItemFrames addObject:OAValueWithRect(itemFrame)];
    }
    layouts[@(ItemFrames)] = skuItemFrames;
    
    viewModel.oa_layouts = layouts;
    
    return CGRectGetMaxY(containerFrame);
}

#pragma mark - Private methods

- (void)skuItemViewModelDidSelect:(RETSkuItemViewModel *)skuItemViewModel {
    NSInteger itemIndex = [self.viewModel itemIndexWithViewModel:skuItemViewModel];
    !self.viewModel.itemDidSelectAtIndex ?: self.viewModel.itemDidSelectAtIndex(self.viewModel, itemIndex);
}

#pragma mark - Event response methods



#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setViewModel:(RETProductSkuCellViewModel *)viewModel {
    _viewModel = viewModel;
    
    self.titleLabel.text = viewModel.title;
    
    for (UIView *subview in self.itemContainer.subviews) {
        [subview removeFromSuperview];
    }
    
    __weak typeof(self) weakSelf = self;
    for (RETSkuItemViewModel *skuItemViewModel in viewModel.items) {
        RETSkuItemView *skuItemView = [RETSkuItemView new];
        skuItemView.viewModel = skuItemViewModel;
        skuItemView.itemDidClick = ^(RETSkuItemViewModel *itemViewModel) {
            [weakSelf skuItemViewModelDidSelect:itemViewModel];
        };
        
        [self.itemContainer addSubview:skuItemView];
    }
}

#pragma mark - Getter

OAGetLabel(titleLabel, ^(UILabel *label){
    label.font = kTitleFont;
    label.textColor = OAColor(333333);
})

OAGetView(itemContainer, ^(UIView *view){
    
})

OAGetMutableArray(itemViews)

#pragma mark - Memory Management Methods

- (void)dealloc {
}

@end
